package com.bltec.emergency.controller;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.Maps;
import com.bltec.emergency.service.mapsService.MapsService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/maps")
@RequiresPermissions("project:maps")
public class MapsController {
    @Resource
    private MapsService mapsService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllMaps")
    public ResponseJson getAllMaps() {
        try {
            List<Maps> maps = mapsService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(maps));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*      private String name;
    private String location;*/
    @RequiresPermissions("maps:insert")
    @RequestMapping("addMaps")
    public ResponseJson addMaps(String name, String location) {
        try {
            Maps maps = new Maps();
            maps.setName(name);
            maps.setLocation(location);
            maps.setEnable(true);
            maps.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            maps.setCreateTime(DateUtil.dateNow());
            mapsService.add(maps);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("maps:update")
    @RequestMapping("updateMaps")
    public ResponseJson updateMaps(Long id,String name, String location) {
        try {
            Maps maps = mapsService.getMapsById(id);
            maps.setName(name);
            maps.setLocation(location);
            maps.setEnable(true);
            maps.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            maps.setUpdateTime(DateUtil.dateNow());
            mapsService.updateMapsById(maps);
            return new ResponseJson(Constant.OK, JSON.toJSON(maps));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }
    @RequiresPermissions("maps:delete")
    @RequestMapping("deleteMaps")
    public ResponseJson setDisable(Long id) {
        if (null != mapsService.getMapsById(id)) {
            if (mapsService.deleteMapsById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
